package com.tianqu.tms.www.proxymember.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.ProxyCarInfo;
import com.tianqu.tms.entity.Sequence;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.proxymember.viewhelper.ProxyMemberCarSearchInputViewHelper;

/**
 * @author zhangxueliang
 * 待办登记 - 信息添加完了controller
 */
public class ProxyMemberCarCreationResultController extends TmsController{
	private Logger logger = Logger
	.getLogger(ProxyMemberCarCreationResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		try{
			String serial = "";
			Sequence sequence = Sequence.getInstance();
			serial = sequence.getSerial(5);//流水号
			ProxyCarInfo proxyCarInfo = ProxyCarInfo.getInstance();
			ProxyMemberCarSearchInputViewHelper viewHelper = (ProxyMemberCarSearchInputViewHelper)
			uc.getAttribute(TmsConstants.VIEWHELP);
			
			proxyCarInfo = viewhelperToEntity(viewHelper,proxyCarInfo);
			TmsUserProfile profile = uc.getUserProfile();
			User user = User.getInstance();
			user = user.getUserInfoById(profile.getUniqueId());
			Member mem = Member.getInstance();//根据当前登入的userId 载入商户id
			if(user.getId() > 0){
				mem = mem.findMemberInfoByUserId(user.getLoginId());
				if(mem.getMemId() > 0){
					proxyCarInfo.setMemberId(String.valueOf(mem.getMemId()));
				}
			}
			proxyCarInfo.setSerial(serial);
			proxyCarInfo.setStatus(1);
			proxyCarInfo.setUpdateUserId(profile.getUniqueId());
			proxyCarInfo.insertProxyCar(proxyCarInfo);
			
			
			
			controller.commit();
			
		}catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}
	
	/**
	 * viewhelper - > entity
	 * @param viewHelper
	 * @param entity
	 * @return
	 * @throws TmsDaoException 
	 */
	private ProxyCarInfo viewhelperToEntity(ProxyMemberCarSearchInputViewHelper viewHelper,
			ProxyCarInfo entity) throws TmsDaoException{
		entity.setVin(viewHelper.getVin());
		entity.setLicensePlate(viewHelper.getLicensePlate());
		entity.setVendorCode(viewHelper.getVendorCode());
		entity.setModelCode(viewHelper.getModelCode());
		entity.setEngine(viewHelper.getEngine());
		if("".equals(viewHelper.getLoadquality())){
			entity.setLoadquality(0);
		}else{
			entity.setLoadquality(Integer.parseInt(viewHelper.getLoadquality()));
		}
		if("".equals(viewHelper.getPassenger())){
			entity.setPassenger(0);
		}else{
			entity.setPassenger(Integer.parseInt(viewHelper.getPassenger()));
		}
		if("".equals(viewHelper.getMileage())){
			entity.setMileage(0);
		}else{
			entity.setMileage(Integer.parseInt(viewHelper.getMileage()));
		}
		
		entity.setYearCarefulPeriod(viewHelper.getYearCarefulPeriod());
		entity.setJqx(viewHelper.getJqx());
		entity.setRegisterCardNo(viewHelper.getRegisterCardNo());
		entity.setRegDate(viewHelper.getRegDate());
		entity.setSendCardDate(viewHelper.getSendCardDate());
		entity.setCarOwner(viewHelper.getCarOwner());
		entity.setCarOwnerIdCardNo(viewHelper.getCarOwnerIdCardNo());
		entity.setOwnerOrgNo(viewHelper.getOwnerOrgNo());
		entity.setOwnerAddress(viewHelper.getOwnerAddress());
		entity.setOwnerPhone(viewHelper.getOwnerPhone());
		entity.setAgentName(viewHelper.getAgentName());
		entity.setAgentAddress(viewHelper.getAgentAddress());
		entity.setAgentOwnerIdCardNo(viewHelper.getAgentOwnerIdCardNo());
		entity.setAgentPhone(viewHelper.getAgentPhone());
		//entity.setPrice(Integer.parseInt(viewHelper.getPrice()));
		entity.setVendorId(viewHelper.getVendorId());
		entity.setBrandId(viewHelper.getBrandId());
		entity.setCarStyleId(viewHelper.getCarStyleId());
		
		Codes code = Codes.getInstance();
		
		entity.setCardType(code.getItemCodeByItemIdAndItemValue(Codes.PLATE_TYPE, viewHelper.getCardType()));
		
		entity.setCarType(code.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_TYPE,viewHelper.getCarType()));
		entity.setCarUse(code.getItemCodeByItemIdAndItemValue(Codes.USER_NATURE,viewHelper.getCarUse()));
		entity.setColor(code.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_COLOR,viewHelper.getColor()));
		entity.setCountryStyle(code.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_COUNTRY,viewHelper.getCountryStyle()));
		entity.setDisplacement(Integer.parseInt(viewHelper.getDisplacement()));
		entity.setGearbox(code.getItemCodeByItemIdAndItemValue(Codes.GEARBOX_TYPE,
				viewHelper.getGearbox()));
		entity.setFuelType(code.getItemCodeByItemIdAndItemValue(Codes.FUEL_TYPE,viewHelper.getFuelType()));
		entity.setOwnerType(code.getItemCodeByItemIdAndItemValue(Codes.CAR_OWNER,viewHelper.getOwnerType()));
		
		
		return entity;
	}

}
